package com.framework2.dialog;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.graphics.drawable.ColorDrawable;
import android.support.annotation.StyleRes;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.framework.utils.ScreenUtils;
import com.framework2.baseEvent.BaseOnClickListener;
import com.yaxin.yyt.R;

/**
 * 自定义的dialog
 *
 * @author YobertJomi
 * className UpdateDialog
 * created at  2017/3/17  10:06
 */
public class ReloginDialog extends AlertDialog {
    private Context context;
    private View viewRoot;
    private TextView tv_pop_title;
    private TextView tv_pop_content;
    private TextView tv_sure;
    private ReloginDialog.OnSureClickListener onclickListener;

    public ReloginDialog(Activity activity) {
        super(activity);
        this.context = activity;
        init();
    }

    public ReloginDialog(Context context, boolean cancelable, OnCancelListener cancelListener) {
        super(context, cancelable, cancelListener);
        init();
    }

    public ReloginDialog(Context context, @StyleRes int themeResId) {
        super(context, themeResId);
        init();
    }

    private void init() {
        viewRoot = LayoutInflater.from(context).inflate(
                R.layout.dialog_relogin, null);
        tv_pop_title = (TextView) viewRoot.findViewById(R.id.tv_pop_title);
        tv_pop_content = (TextView) viewRoot.findViewById(R.id.tv_pop_content);
        tv_sure = (TextView) viewRoot.findViewById(R.id.tv_sure);

//        int height=(int)((ScreenUtils.getInstance().getScreenHeightPx(context)-ScreenUtils.getInstance()
// .getStatusBarHeightPx(context)-ScreenUtils.getInstance().dip2px(context,50))*0.34);
//        PercentRelativeLayout.LayoutParams layoutParams=new PercentRelativeLayout.LayoutParams((int)(ScreenUtils
// .getInstance().getScreenWidthPx(context)*0.8),height);
//        viewRoot.findViewById(R.id.rootView).setLayoutParams(layoutParams);
        RelativeLayout.LayoutParams layoutParams = new RelativeLayout.LayoutParams(ScreenUtils.getInstance()
                .getScreenWidthPx(context), ScreenUtils.getInstance().getScreenHeightPx(context));
        viewRoot.setLayoutParams(layoutParams);
    }

    private void initEvent() {
        tv_sure.setOnClickListener(new BaseOnClickListener() {
            @Override
            public void onBaseClick(View v) {
                if (null != onclickListener) {
                    onclickListener.onSureClickListener("");
                }
            }
        });
    }

    public ReloginDialog setOnSureClickListener(ReloginDialog.OnSureClickListener onclickListener) {
        this.onclickListener = onclickListener;
        return this;
    }

    /**
     * 显示dialog
     */
    public void showDialog() {
        initEvent();
        show();
        Window window = getWindow();
        if (null != window) {
            WindowManager.LayoutParams layoutParams = window.getAttributes();
            layoutParams.width = ScreenUtils.getInstance().getScreenWidthPx(getContext());
            window.setAttributes(layoutParams);
            window.setGravity(Gravity.CENTER);

            window.setBackgroundDrawable(new ColorDrawable(0));
            window.setContentView(viewRoot);
            setCancelable(false);
            setCanceledOnTouchOutside(false);
        }
    }

    public interface OnSureClickListener {
        void onSureClickListener(String info);
    }
}
